package com.jmb.api.controller.login;

import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.Result;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.service.PermissionRoleService;
import com.jmb.api.service.UserService;
import com.jmb.api.vo.login.LoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@Tag(name = "后台管理系统登录管理")
@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionRoleService permissionRoleService;
    /**
     * 登录
     * @param loginVo
     * @return
     */
    @PostMapping("user/login")
    @Operation(summary = "登录")
    public Result<HashMap<String,Object>> login(@RequestBody @Valid LoginVo loginVo, HttpServletRequest request){
        HashMap<String, Object> login = userService.login(loginVo,request);
        return Result.ok(login);
    }

    /**
     * 退出
     * @return
     */
    @Operation(summary = "退出登陆")
    @GetMapping("user/logout")
    public Result<String> logout(@RequestHeader("token") String token){
        if (token==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        userService.logout(token);
        return Result.ok();
    }

    /**
     * 注册
     * @return
     */
    public Result<String> register(@RequestBody LoginVo loginVo){

        return Result.ok();
    }
}
